from datetime import datetime
from enum import Enum
from typing import List

from sqlalchemy import DateTime, String, Text, Enum as SQLEnum
from sqlalchemy.orm import Mapped, mapped_column, relationship

from .engine import Base


# 书籍状态
class BookStatus(Enum):
    UPDATING = "updating",  # 更新中
    COMPLETED = "completed",  # 已完结
    DISCONTINUED = "discontinued",  # 断更
    UNKNOWN = "unknown"  # 未知


class Book(Base):
    __tablename__ = 'book'
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    name: Mapped[str] = mapped_column(String(128), comment="书名")
    author: Mapped[str | None] = mapped_column(String(64), comment="作者")
    status: Mapped[BookStatus] = mapped_column(SQLEnum(BookStatus), comment="书籍状态")
    description: Mapped[str | None] = mapped_column(String(2000), comment="书籍描述信息")
    updated: Mapped[datetime | None] = mapped_column(DateTime, comment="更新时间")
    url: Mapped[str] = mapped_column(String(1024), unique=True, comment="爬虫获取的链接")
    page_source: Mapped[str | None] = mapped_column(Text, comment="网页源代码")
    chapters: Mapped[List["Chapter"]] = relationship("Chapter", primaryjoin="Book.id == Chapter.book_id",
                                                     back_populates="book")
